Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid raising bare Exception #1168

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

zaicruvoir1rominet
Copy link
Contributor

@zaicruvoir1rominet zaicruvoir1rominet commented Jul 4, 2024

Summary

Before tackling issue #457, there are some bare Exceptions thrown from within the code.

Here is the logic for changes in Exceptions:

  • use ValueError when issue caused by user input
  • use CSTValidationError & TypeError when issue occurs during the CST validation process
  • use ParserSyntaxError when issue occurs during the parsing process
  • use CSTLogicError to replace all Exception which contained "Logic error" in the cause message
  • remove Exception catch+re-raise when not needed

I'm all for changes if some Exceptions changes are not OK,
In particular, there are some ParserSyntaxError which may not be meaningful - with raw_line=0, raw_column=0 params

Test Plan

Keep the current test plan.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jul 4, 2024
Copy link

codecov bot commented Jul 4, 2024

Codecov Report

Attention: Patch coverage is 25.49020% with 114 lines in your changes missing coverage. Please review.

Project coverage is 91.29%. Comparing base (8b97600) to head (eb3f2a6).
Report is 9 commits behind head on main.

Files Patch % Lines
libcst/matchers/_matcher_base.py 9.52% 19 Missing ⚠️
libcst/_parser/conversions/expression.py 18.18% 9 Missing ⚠️
libcst/_parser/conversions/statement.py 18.18% 9 Missing ⚠️
...bcst/codemod/commands/convert_format_to_fstring.py 20.00% 8 Missing ⚠️
libcst/_parser/parso/python/tokenize.py 12.50% 7 Missing ⚠️
libcst/_parser/conversions/params.py 14.28% 6 Missing ⚠️
libcst/codegen/gen_matcher_classes.py 16.66% 5 Missing ⚠️
libcst/helpers/_template.py 0.00% 5 Missing ⚠️
libcst/_nodes/base.py 20.00% 4 Missing ⚠️
libcst/_parser/grammar.py 0.00% 4 Missing ⚠️
... and 19 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1168      +/-   ##
==========================================
+ Coverage   91.26%   91.29%   +0.03%     
==========================================
  Files         261      262       +1     
  Lines       26877    26899      +22     
==========================================
+ Hits        24529    24558      +29     
+ Misses       2348     2341       -7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@kiri11
Copy link
Contributor

kiri11 commented Jul 27, 2024

This seems like an improvement, thank you. It's really hard to catch a specific Exception, I have to rely on its message. I hope this gets merged.

Copy link
Member

@zsol zsol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this! I've got a few minor comments but overall looks great!

Comment on lines +1 to +8
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.


class CSTLogicError(Exception):
pass
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should go in _exceptions.py, and please write a docstring

Copy link
Contributor Author

@zaicruvoir1rominet zaicruvoir1rominet Aug 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review !

I'm going to need help on this one though:

  • It's in a separate _excep.py file, since _exceptions.py already imports internals, which in turn may raise a CSTLogicError.
    Do you see any other ways than my (I admit lazy) solution in order to avoid circular import errors ?
  • For the docstring, I couldn't think of anything meaningful, other than "Well... it's a CST Logic error", which doesn't add more info or context than the original name. I'd be glad to add a real doc if you have something better in mind :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zsol
Any updates ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_exceptions.py already imports internals, which in turn may raise a CSTLogicError.

Let's move get_expected_str out of _exceptions.py. That should help.

For the docstring, I couldn't think of anything meaningful

How about:

General purpose internal error within LibCST itself.

libcst/codemod/visitors/_add_imports.py Outdated Show resolved Hide resolved
libcst/codemod/visitors/_remove_imports.py Show resolved Hide resolved
libcst/codemod/visitors/_remove_imports.py Outdated Show resolved Hide resolved
libcst/metadata/type_inference_provider.py Outdated Show resolved Hide resolved
@zaicruvoir1rominet
Copy link
Contributor Author

Thanks for the support @kiri11 !
You also made me realize some users were perhaps using exception messages in order to manage errors, so I kept all original Exception messages in order to avoid breaking changes.

I hope everything is now ready (apart from that CSTLogicError file location & docstring)

@kiri11
Copy link
Contributor

kiri11 commented Aug 12, 2024

You're welcome! (although I didn't do anything)
Keeping the exception messages backwards-compatible is a great idea, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants